import java.util.Arrays;
import java.util.Scanner;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a1227
 * Date: 2022-08-20
 * Time: 20:50
 */

/**
 * 2022/8/29 练习
 */
public class Text {
    /**
     * 链表的回文结构——https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking
     * @param A 链表的头节点
     * @return 是否是回文结构
     */
    /*
    public boolean chkPalindrome(ListNode A) {
        //链表为空
        if(A == null){
            return false;
        }
        // write code here
        ListNode fast = A;
        ListNode slow = A;
        //先确定中间节点slow
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        //反转中间节点后半部分的节点
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断链表是否为回文
        while(A != slow){//链表节点为奇数的情况
            if(A.val != slow.val){
                return false;
            }
            A = A.next;
            slow = slow.next;
            //链表节点为偶数的情况
            if(A.next == slow){
                return true;
            }
        }
        return false;
    }
    */

    /**
     * 相交链表-https://leetcode.cn/problems/intersection-of-two-linked-lists/
     * @param headA 链表A的头节点
     * @param headB 链表B的头节点
     * @return 两个单链表相交的起始节点
     */
    /*
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //假定long存储的是长链表
        ListNode longlist = headA;
        //假定short存储的短链表
        ListNode shortlist = headB;
        //计算headA的长度
        int lenA = 0;
        while(longlist != null){
            lenA++;
            longlist = longlist.next;
        }
        //计算headB的长度
        int lenB = 0;
        while(shortlist != null){
            lenB++;
            shortlist = shortlist.next;
        }
        //long、short经过遍历以为空，重新引用
        longlist = headA;
        shortlist = headB;
        //判断lenA-lenB的差值，看long、short引用的链表是否如其名
        int len = lenA - lenB;
        //交换长短引用
        if(len < 0){
            longlist = headB;
            shortlist = headA;
            len = lenB - lenA;
        }
        //先让long走len步
        while(len != 0){
            longlist = longlist.next;
            len--;
        }
        //让长短一起走，长短相同则为相交的起始节点
        while(longlist != shortlist){
            longlist = longlist.next;
            shortlist = shortlist.next;
        }
        return longlist;
    }
     */

    /**
     * 环形链表-https://leetcode.cn/problems/linked-list-cycle/
     * @param head 链表的头节点
     * @return 链表中是否存在环
     */
    /*
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        //循环直到相遇
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            //存在环
            if(fast == slow){
                return true;
            }
        }
        //不存在环
        return false;
    }
     */

    /**
     * 环形链表 II-https://leetcode.cn/problems/linked-list-cycle-ii/
     * @param head 链表的头节点
     * @return 链表开始入环的第一个节点
     */
    /*
    public ListNode detectCycle(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //找在环中的相遇点
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        //判断是否存在环
        if(fast == null || fast.next == null){
            return null;
        }
        //从相遇点和开头一次一步，直到相遇，则相遇点为入点
        slow = head;
        while(slow != fast){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
     */

    public static void main(String[] args) {

    }
}
